package auditoryUIbk;

import java.awt.*;
import java.awt.geom.Rectangle2D;

import javax.swing.*;

public class JSampleViewer extends JComponent {
	
	float[] sample;
	float samplemax, samplemin;
	
	int xzoom = 10, yzoom = 10;
	
	public static void view(float[] sample) {
		JSampleViewer c = new JSampleViewer(sample);
		JFrame f = new JFrame();
		f.getContentPane().add(c);
		f.pack();
		f.setVisible(true);
	}
	
	public static void view(byte[] sample) {
		JSampleViewer c = new JSampleViewer(sample);
		JFrame f = new JFrame();
		f.getContentPane().add(c);
		f.pack();
		f.setVisible(true);
	}
	
	public JSampleViewer(float[] sample) {
		super();
		this.sample = sample;
		samplemax = 1;
		samplemin = -1;
	}
	
	public JSampleViewer(byte[] sample) {
		super();
		this.sample = new float[sample.length];
		for (int i=0; i<sample.length; i++)
			this.sample[i] = sample[i];
		samplemax = 127;
		samplemin = -128;
	}
	
	public Dimension getPreferredSize() {
		return new Dimension(600, 400);
	}
	
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		g2.setColor(Color.gray);
		g2.fill(getBounds());
		g2.setColor(Color.blue);
//		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int w = getWidth();
		int h = getHeight();
		for (int x=0; x<w; x++) {
			float y = (sample[x*sample.length/w/xzoom] - samplemin)/(float)(samplemax - samplemin);
			g2.drawLine(x, h/2, x, h/2 + (int)((0.5f-y)*h*yzoom));
		}
	}
	
	
}
